<?php

namespace App\Http\Controllers\Settle;

use App\Http\Controllers\BaseController;
use App\Http\Validate\Settle\SettleCompany as SettleSettleCompany;
use App\Models\Settle\SettleCompany;
use Illuminate\Http\Request;

/**
 * 结算公司管理相关
 */
class SettleCompanyController extends BaseController
{
    /**
     * 获取列表
     */
    public function getlist(Request $request)
    {
        //需获取当前用户所属账套
        $status = $request->status ?? 0;
        $isgetall = $request->isgetall ?? 0;
        $list = SettleCompany::getList($request, $this->mangerinfo['cpid'], $status, $isgetall);
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $list]);
    }

    /**
     * 新增
     */
    public function dopostadd(Request $request)
    {
        $req_data = $request->only(['settle_no', 'settle_company_name','account','open_bank','address']);

        $Validate = new SettleSettleCompany();
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $isexist = SettleCompany::where([['settle_no', $req_data['settle_no']],['cpid' , $this->mangerinfo['cpid']]])->first();

        if ($isexist) {
            return response()->json(['code' => '001', 'msg' => '编码不允许重复']);
        }

        $data = [
            'cpid' => $this->mangerinfo['cpid'],
            'settle_no' => $req_data['settle_no'],
            'settle_company_name' => $req_data['settle_company_name'],
            'account' => $req_data['account'],
            'open_bank' => $req_data['open_bank'],
            'address' => $req_data['address'],
            'update_at' => time()
        ];
        $data['create_at'] = time();
        SettleCompany::create($data)->id;
        return response()->json(['code' => '000', 'msg' => '新增成功']);
    }

    /**
     * 编辑
     */
    public function dopostedit(Request $request)
    {
        $id = $request->id ?? 0;
        $info = SettleCompany::where([['id', $id], ['cpid', $this->mangerinfo['cpid']],['status',1]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }

        $req_data = $request->only(['settle_no', 'settle_company_name','account','open_bank','address']);

        $Validate = new SettleSettleCompany();
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $isexist = SettleCompany::where([['id','<>', $id],['settle_no', $req_data['settle_no']],['cpid' , $this->mangerinfo['cpid']]])->first();

        if ($isexist) {
            return response()->json(['code' => '001', 'msg' => '编码不允许重复']);
        }


        $data = [
            'settle_no' => $req_data['settle_no'],
            'settle_company_name' => $req_data['settle_company_name'],
            'account' => $req_data['account'],
            'open_bank' => $req_data['open_bank'],
            'address' => $req_data['address'],
            'update_at' => time()
        ];
        SettleCompany::where([['id', $id], ['cpid', $this->mangerinfo['cpid']]])->update($data);

        return response()->json(['code' => '000', 'msg' => '编辑成功']);
    }

    /**
     * 冻结
     */
    public function changestatus(Request $request)
    {

        $id = $request->id ?? 0;
        $info = SettleCompany::where([['id', $id], ['cpid', $this->mangerinfo['cpid']]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        $status = 2;
        SettleCompany::where([['id', $id]])->update(['status' => $status, 'update_at' => time()]);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }
}
